package com.doom.a3rdlibdemo;

import android.util.Log;

public class ObservableCreate<T> extends Observable<T> {

    private static final String TAG = ObservableCreate.class.getSimpleName();
    ObservableOnSubscribe<T> source;

    public ObservableCreate(ObservableOnSubscribe<T> source){
        this.source = source;
    }

    @Override
    public void subscribe(Observer observer) {
        subscribeActual(observer);
    }

    @Override
    protected void subscribeActual(Observer<T> observer) {
//        observer.onNext(observer);
        CreateEmitter<T> parent = new CreateEmitter<>(observer);
        Log.d(TAG, "subscribeActual this:"+this);
        Log.d(TAG, "subscribeActual observer:"+observer);
        observer.onSubscribe();
        Log.d(TAG, "subscribeActual onSubscribe");

        source.subscribe(parent);
    }

    static final class CreateEmitter<T> implements Emitter<T> {

        Observer<T> observer;

        public CreateEmitter(Observer<T> observer) {
            this.observer = observer;
        }

        @Override
        public void onNext(T t) {
            observer.onNext(t);
        }

        @Override
        public void onError(Throwable throwable) {
            observer.onError(throwable);
        }

        @Override
        public void onComplete() {
            observer.onComplete();
        }
    }
}
